package paneles;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import peluqueria.Peluqueria;

public class cTurnos extends DefaultTableCellRenderer {
	
	private Color cLibre;
	private Color cTurnoCorte;
	private Color cInhabilitado;
	private Color cTurnoColor;
	
	public cTurnos(){
		Properties p = new Properties();
		FileInputStream fis;
		try {
			fis = new FileInputStream("propiedades.properties");
			p.load(fis);
			fis.close();
			int r,g,b;
			long valor = Long.valueOf(((String)p.get(new String("colorlibre")))).longValue();
			b = (int) (valor%256);
			long aux = valor/256;
			g = (int) (aux%256);
			aux = aux/(256);
			r = (int) (aux%256);
			cLibre = new Color(r,g,b);
			
			valor = Long.valueOf(((String)p.get(new String("colorturnocorte")))).longValue();
			b = (int) (valor%256);
			aux = valor/256;
			g = (int) (aux%256);
			aux = aux/(256);
			r = (int) (aux%256);
			cTurnoCorte = new Color(r,g,b);
			
			valor = Long.valueOf(((String)p.get(new String("colorturnocolor")))).longValue();
			b = (int) (valor%256);
			aux = valor/256;
			g = (int) (aux%256);
			aux = aux/(256);
			r = (int) (aux%256);
			cTurnoColor = new Color(r,g,b);
			
			valor = Long.valueOf(((String)p.get(new String("colorinhabilitado")))).longValue();
			b = (int) (valor%256);
			aux = valor/256;
			g = (int) (aux%256);
			aux = aux/(256);
			r = (int) (aux%256);
			cInhabilitado = new Color(r,g,b);
			
		} catch (FileNotFoundException e) {
			this.cInhabilitado = new Color(255,0,0);
			this.cLibre = new Color(0,150,0);
			this.cTurnoColor = new Color(0,153,153);
			this.cTurnoCorte = new Color(204,204,0);
			//JOptionPane.showMessageDialog(Peluqueria.getInstance().getJFrame(), "No se encuentra el archivo de configuracion.", "Error", JOptionPane.ERROR_MESSAGE);

		} catch (IOException e) {

		}

	}

	  public Component getTableCellRendererComponent(JTable table,
			      Object value,
			      boolean isSelected,
			      boolean hasFocus,
			      int row,
			      int column){
		  super.getTableCellRendererComponent (table, value, isSelected, hasFocus, row, column);
		  if(row == 0){
			  ((JLabel) table.getDefaultRenderer(String.class)).setHorizontalAlignment(JLabel.LEFT);	  
		  }
		  if (isSelected && hasFocus){
			  ((JComponent)table.getCellRenderer(row, column)).setBorder(BorderFactory.createLineBorder(new Color(0,0,0), 2));
		  }
		  if(value != null){
			  this.setOpaque(true);
			  this.setFont(new Font("Verdana", 1, 11));
			  this.setHorizontalAlignment(JLabel.CENTER);
			  this.setForeground(Color.WHITE);
			  if(((Celda)value).getComentario() != null){
				  if(((Celda)value).getComentario().equals("Reflejos"))
					  this.setIcon(new ImageIcon("reflejo.png"));
				  else
					  this.setIcon(new ImageIcon("icono.png"));
			  }
			  else
				  this.setIcon(null);
			  if (value.toString().equals("Libre")){
				  this.setBackground(this.cLibre);
			  }
			  else{
				  if(value.toString().equals("<html><p align= 'center'><style='line-height=-30%'>No<br>Disponible</s></p></html>")){
					  this.setBackground(this.cInhabilitado);
					  this.setFont(new Font("Verdana",0,9));
				  }
				  else
				  {
					  if(value instanceof Celda){
						  if(((Celda)value).getTipo_servicio().equals("Corte"))
							  this.setBackground(this.cTurnoCorte);
						  else
							  this.setBackground(this.cTurnoColor);
					 					  
						  if((column != 0) && (column != table.getColumnCount()-1)){
							  this.setFont(new Font("Verdana", 0, 9));
						  }
						  else{
							  if(column == 0){
								  this.setBackground(this.cTurnoCorte);
							  }
							  else{
								  this.setBackground(this.cTurnoColor);
							  }
							  this.setForeground(Color.BLACK);
							  this.setOpaque(true);
						  }
						 
					  }
				  }
			  }
		  }
		  else{
			  this.setBackground(new Color(0,0,0));
			  this.setForeground(Color.WHITE);
		  }
		  
		  return this;
	}
	  
	public boolean isCellEditable(int row, int column){
		if(column == 0)
			return false;
		return true;
	}

	public Color getCLibre() {
		return cLibre;
	}

	public void setCLibre(Color libre) {
		cLibre = libre;
	}

	public Color getCOcupado() {
		return cTurnoCorte;
	}

	public void setCOcupado(Color turnoCorte) {
		cTurnoCorte = turnoCorte;
	}

	public Color getCInhabilitado() {
		return cInhabilitado;
	}

	public void setCInhabilitado(Color inhabilitado) {
		cInhabilitado = inhabilitado;
	}

}
